package com.whp.openapi.web.controller;

import com.whp.openapi.common.Const;
import com.whp.openapi.common.ResponseResult;
import com.whp.openapi.common.ResultTable;
import com.whp.openapi.entity.AdminUser;
import com.whp.openapi.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author whp
 * @date 2021/11/23
 */
@RestController
@RequestMapping("/user")
public class AdminUserController {

    @Autowired
    private AdminUserService userService;

    /**
     * 登录
     * @param email
     * @param password
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public ResponseResult login(String email, String password, HttpSession session) {
        AdminUser user = userService.doLogin(email, password);
        System.out.println(user);
        if (user != null && user.getPassword().equals(password)) {
            session.setAttribute(Const.IS_LOGIN, user);
            return ResponseResult.getSuccess();
        }
        return ResponseResult.getFail("用户名或密码错误！");
    }

    /**
     * 注销
     * @param session
     * @param response
     * @throws IOException
     */
    @RequestMapping("/logout")
    public void logout(HttpSession session, HttpServletResponse response) throws IOException {
        //会话无效
        session.invalidate();
        //手动进行重定向至登录界面
        response.sendRedirect("/login.html");
    }

    /**
     * 用户管理：搜索、分页
     * @param page
     * @param limit
     * @param realName
     * @param email
     * @param status
     * @return
     */
    @RequestMapping("/table")
    public ResultTable table(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer limit,
                             String realName,
                             String email,
                             Integer status){
        return userService.table(page, limit, realName, email, status);
    }

    /**
     * 获取用户（授权）
     * @param userId
     * @return
     */
    @RequestMapping("/user_role")
    public List<Integer> getUserRole(Integer userId){
        return userService.getUserRole(userId);
    }

    /**
     * 修改用户权限（授权）
     * @param userId
     * @param roleIds
     * @return
     */
    @RequestMapping("/assign_role")
    public ResponseResult assignRole(Integer userId, Integer[] roleIds){
        try {
            return userService.assignRole(userId, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("操作失败");
    }

    /**
     * 删除用户
     * @param ids
     * @return
     */
    @RequestMapping("/del")
    public ResponseResult delUsers(Integer[] ids){
        try {
            return userService.delUsers(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("删除失败");
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @RequestMapping("/add")
    public ResponseResult addUser(AdminUser user){
        try {
            return userService.addUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("添加失败");
    }

    /**
     * 编辑（修改用户信息）
     * @param user
     * @return
     */
    @RequestMapping("/update")
    public ResponseResult updateUser(AdminUser user){
        try {
            return userService.updateUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("修改失败");
    }
}
